package cn.powerone.scm.controller;

import java.util.ArrayList;
import java.util.List;

import cn.powerone.scm.domain.ScmInventoryView;
import cn.powerone.scm.domain.ScmInventoryWarehouseLocation;
import cn.powerone.scm.domain.ScmProducts;
import cn.powerone.scm.service.IScmInventoryWarehouseLocationService;
import cn.powerone.scm.service.IScmProductsService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.powerone.common.annotation.Log;
import cn.powerone.common.core.controller.BaseController;
import cn.powerone.common.core.domain.AjaxResult;
import cn.powerone.common.enums.BusinessType;
import cn.powerone.scm.domain.ScmInventory;
import cn.powerone.scm.service.IScmInventoryService;
import cn.powerone.common.utils.poi.ExcelUtil;
import cn.powerone.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author PowerOne
 * @date 2025-07-20
 */
@RestController
@RequestMapping("/scm/inventory")
public class ScmInventoryController extends BaseController
{
    @Autowired
    private IScmInventoryService scmInventoryService;
    @Autowired
    private IScmInventoryWarehouseLocationService scmInventoryWarehouseLocationService;

    @Autowired
    private IScmProductsService scmProductsService;
    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('scm:inventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScmInventory scmInventory)
    {
        startPage();
        List<ScmInventory> list = scmInventoryService.selectScmInventoryList(scmInventory);

        // 假设有 IWarehouseLocationService 和 IProductService
        List<ScmInventoryView> viewList = new ArrayList<>();
        for (ScmInventory inv : list) {
            ScmInventoryView view = new ScmInventoryView();
            BeanUtils.copyProperties(inv, view);

            // 查询库位名称
            ScmInventoryWarehouseLocation location = scmInventoryWarehouseLocationService.selectScmInventoryWarehouseLocationById(inv.getWarehouseLocationId());
            if (location != null) {
                view.setWarehouseLocationName(location.getLocationName());
            }

            // 查询产品信息
            ScmProducts product = scmProductsService.selectScmProductsById(inv.getProductId());
            if (product != null) {
                view.setProductName(product.getProductName());
                view.setProductDrawing(product.getDrawingNumber());
                view.setProductVersion(product.getVersion());
                view.setProductInnerVersion(String.valueOf(product.getInternalVersion()));
            }

            viewList.add(view);
        }
        return getDataTable(viewList);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('scm:inventory:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScmInventory scmInventory)
    {
        List<ScmInventory> list = scmInventoryService.selectScmInventoryList(scmInventory);
        ExcelUtil<ScmInventory> util = new ExcelUtil<ScmInventory>(ScmInventory.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('scm:inventory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scmInventoryService.selectScmInventoryById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('scm:inventory:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScmInventory scmInventory)
    {
        return toAjax(scmInventoryService.insertScmInventory(scmInventory));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('scm:inventory:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScmInventory scmInventory)
    {
        return toAjax(scmInventoryService.updateScmInventory(scmInventory));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('scm:inventory:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scmInventoryService.deleteScmInventoryByIds(ids));
    }
}
